/**   
* @{#} ListOpt.java Create on 2013-8-6 上午10:30:24   
*   
* Copyright (c) 2012 by Baidu.   
*/
package objtable.algm;

import java.util.HashSet;
import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

/**   
* @author <a href="mailto:zhangbo07@baidu.com">zhangbo07</a>  
* @version 1.0   
*/

public class ListOpt {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List intersect(List a, List b) {
		HashSet seta = new HashSet(a);
		HashSet setb = new HashSet(b);
		SetView sv = Sets.intersection(seta, setb);
		List result = Lists.newArrayList(sv.toArray());
		return result;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List union(List a, List b) {
		HashSet seta = new HashSet(a);
		HashSet setb = new HashSet(b);
		SetView sv = Sets.union(seta, setb);
		List result = Lists.newArrayList(sv.toArray());
		return result;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List diff(List a, List b) {
		HashSet seta = new HashSet(a);
		HashSet setb = new HashSet(b);
		SetView sv = Sets.difference(seta, setb);
		List result = Lists.newArrayList(sv.toArray());
		return result;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List diffAll(List a, List b) {
		HashSet seta = new HashSet(a);
		HashSet setb = new HashSet(b);
		SetView sv = Sets.union(seta, setb);
		SetView sv1 = Sets.intersection(seta, setb);
		SetView sv2 = Sets.difference(sv, sv1);
		List result = Lists.newArrayList(sv2.toArray());
		return result;
	}

}
